/*
Input: newdata/estimation_prep_ltw18 [prepared precinct-level panel]
	
Output: Figure 7


*/	
	
* PULL: Precinct-level data
	use "$newdata/estimation_prep_ltw18.dta", clear
	
	** means of outcomes (weighted by #eligible voters)
	su turnout_urne turnout_pos_req turnout_tot_req share_mail [aw=wahlber_gesamt]
		
	
********************************************************************************
 //	 Baseline: Event Study Illustration TWFE 4-way figure (Figure 7) //
********************************************************************************	
	
	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen F`l'event = K==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K==`l'
	}
	order F1event, last
	
	// Estimate baseline ES
	estimates clear
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req share_mail treat_simple ln_street_dist del_street_dist	{
		 reghdfe `v' F7event-L7event F1event $ctr $wgt if smpl_trim==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
		
		estimates store `v'
	}
		
	// PLOT: Treatment intensity
	event_plot  treat_simple del_street_dist, ///
	stub_lag(L#event ) stub_lead(F#event ) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.1(0.2)0.1) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Share reassignments""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) order(1 "Reassigned" 3 "Change in walking distance (km)" ) row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel A.} Treatment Intensity",nobox span bexpand justification(left) size(medium)) ///
		/*name(treatment, replace)*/ ) ///
	lag_opt1(msymbol(O) msize(3pt) color(black)) 	lag_ci_opt1(color(black)) ///
	lag_opt2(msymbol(O) msize(3pt) color(maroon)) lag_ci_opt2(color(maroon)) noplot savecoef
	
	// ---> Plot same thing with 2 yaxes for reassignments and distance (savecoef above)
	tw  (scatter __event_coef1 __event_pos1 , sort yaxis(1) lcol(black) ms(Dh) msize(2.5pt) mcol(black))   (rcap __event_lo1 __event_hi1 __event_pos1, yaxis(1) col(black)) ///
		(scatter __event_coef2 __event_pos2 , sort yaxis(2) lcol(maroon) ms(Dh) msize(2.5pt) mcol(maroon)) (rcap __event_lo2 __event_hi2 __event_pos2, yaxis(2) col(maroon)) ///
		, ytitle("Share reassignments""(estimates)", size(small)) ytitle("Change in distance in km""(estimates)", axis(2) size(small) orient(rvertical) ) ///
		xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ysc(r(-.1 .85) axis(2)) ysc(r(-.1 .85) axis(1)) ylab(#6, axis(2)) ylab(#6, axis(1)) ///
		legend(pos(12) order(1 "Reassigned" 3 "Change in walking distance (km)" ) row(1) region(style(none))) ///
		xline(-0.5, lcol(black) lpat(solid)) yline(0, lcolor(gray) lpat(solid)) name(treatment, replace) ///
		title("{bf:Panel A.} Treatment Intensity",nobox span bexpand justification(left) size(medium)) 
				
	// PLOT: Share Mail-in
	event_plot  share_mail, ///
	stub_lag(L#event ) stub_lead(F#event ) plottype(connect) ciplottype(rcap) ///
	together  trimlead(4) trimlag(2)  noautolegend ///
	graph_opt(ytitle("Share of mail-in votes in %""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) order(1 "Share of mail-in votes in total votes" ) row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel B.} Effect on Mode of Voting" ,nobox span bexpand justification(left) size(medium)) ///
		name(share_mail,replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(black) lpat(_)) lag_ci_opt1(color(black))
	
	// PLOT: Turnouts Urne + Postal	
	event_plot  turnout_urne turnout_pos_req, ///
	stub_lag(L#event ) stub_lead(F#event ) plottype(connect) ciplottype(rcap) ///
	together perturb(-0.1(0.2)0.1) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) order(1 "Polling place turnout" 3 "Mail-in turnout" ) row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel C.} Effect on Mail-in and Polling Place Turnout",nobox span bexpand justification(left) size(medium)) ///
		name(urne_postal, replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(navy))  		 lag_ci_opt1(color(navy)) ///
	lag_opt2(msymbol(O) msize(3pt) color(maroon) lpat(-)) lag_ci_opt2(color(maroon))  

	// PLOT: Turnouts Total
	event_plot  turnout_tot_req, ///
	stub_lag(L#event ) stub_lead(F#event ) plottype(connect) ciplottype(rcap) ///
	together trimlead(4) trimlag(2)  noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) order(1 "Total turnout") row(1) region(style(none))) ///
		title("{bf:Panel D.} Effect on Total Turnout",nobox span bexpand justification(left) size(medium)) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		name(turnout_tot_req,replace) ) ///
	lag_opt1(msymbol(Oh) msize(3pt) color(black)) 	lag_ci_opt1(color(black)) 	 
		
	* PLOT: FIGURE 7. The Effect of Reassignments on Turnout and the Mode of Voting
	graph combine treatment share_mail urne_postal turnout_tot_req, xcommon  col(2)	iscale(.7)	
	gr_edit .style.editstyle declared_ysize(4.25) editcopy	
	graph export "$figures/Figure_7_ES_baseline.pdf", replace
	
	
	*** Test if post-treatment coefs for tot turnout are different
	reghdfe turnout_tot_req F7event-L7event F1event $ctr $wgt if smpl_trim==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)

	// test if third and second post-treatment coef are different
	test 	L2event=L1event
	// test if third and second post-treatment coefs are jointly equal to zero 
	test 	(L2event=0) (L1event=0)
	test 	L2event=L1event=0

	
